package admin

import (
	"encoding/base64"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
	"tcsc/models"
	"tcsc/sword"
	"time"

	"github.com/astaxie/beego"
)

func Dir(style, dirname, timestamp2, final string) (savepath, pathname, path, dirpath string) {
	savepath = "static/uppic/" + style + "/" + dirname + "/" + timestamp2 + "." + final
	pathname = "/static/uppic/" + style + "/" + dirname + "/" + timestamp2 + "." + final
	path = "/static/uppic/" + style + "/" + dirname
	dirpath = "./static/uppic/" + style + "/" + dirname
	return
}

type ImgsAjax struct {
	beego.Controller
}

//根据上传的地方进行分类存放图片
func (c *ImgsAjax) Post() {
	inputs := c.Input()
	b64 := inputs.Get("tp")
	base := b64[strings.IndexByte(b64, ',')+1:]
	res, _ := base64.StdEncoding.DecodeString(base)
	timestamp := time.Now().UnixNano()
	timestamp2 := strconv.FormatInt(timestamp, 10)
	timestamp11 := time.Now().Unix()
	tm := time.Unix(timestamp11, 0)
	dirname := tm.Format("2006-01-02")
	var where string
	if inputs.Get("where") == "" {
		where = "default"
	} else {
		where = inputs.Get("where")
	}
	savepath, _, path, dirpath := Dir(where, dirname, timestamp2, "png")
	if errdir := os.MkdirAll(dirpath, 0777); errdir != nil {
		beego.Debug(errdir)
	}

	if err := ioutil.WriteFile(savepath, res, 0666); err != nil {
		beego.Debug("打开文件失败:", err)
	}
	// defer os.Remove(localpath)
	osspath, err := models.OssUpfile(savepath, savepath)
	if err != nil {
		beego.Debug(err)
	}

	var img models.Mt_resources
	sess := c.GetSession("admin")
	if sess != nil {
		img.Uid, _ = strconv.Atoi(sess.(string))
	} else {
		img.Uid = 0
	}
	img.Name = timestamp2 + ".png"
	img.Path = path
	img.Pathname = osspath
	// img.Addtime = time.Now()
	img.Addtime = sword.Timeget()
	img.Ip = strings.Split(c.Ctx.Request.RemoteAddr, ":")[0]
	mid, err := models.Orm().Insert(&img)
	var json = make(map[string]interface{})
	if err != nil {
		beego.Debug("insert media数据失败:", err)
		json["status"] = "0"
	} else {
		json["status"] = "1"
		json["mid"] = mid
		json["pathname"] = osspath
	}
	c.Data["json"] = json
	c.ServeJSON()
}

type File struct {
	beego.Controller
}

//根据上传的地方进行分类存放图片
func (c *File) Post() {
	defer c.ServeJSON()
	var js = make(map[string]interface{})
	c.Data["json"] = &js
	f, n, errup := c.GetFile("file")
	defer f.Close()
	if errup != nil {
		beego.Debug("上传文件失败:", errup)
		js["status"] = 0
	} else {
		final := strings.Split(n.Filename, ".")[1]
		timestamp := time.Now().UnixNano()
		timestamp2 := strconv.FormatInt(timestamp, 10)
		timestamp11 := time.Now().Unix()
		tm := time.Unix(timestamp11, 0)
		dirname := tm.Format("2006-01-02")
		var where string
		if c.GetString("where") == "" {
			where = "default"
		} else {
			where = c.GetString("where")
		}
		savepath, _, path, dirpath := Dir(where, dirname, timestamp2, final)
		if errdir := os.MkdirAll(dirpath, 0777); errdir != nil {
			beego.Debug(errdir)
		}
		c.SaveToFile("file", savepath)
		// defer os.Remove(localpath)
		osspath, err := models.OssUpfile(savepath, savepath)
		var img models.Mt_resources
		sess := c.GetSession("home")
		if sess != nil {
			img.Uid, _ = strconv.Atoi(sess.(string))
		} else {
			img.Uid = 0
		}
		img.Ip = strings.Split(c.Ctx.Request.RemoteAddr, ":")[0]
		img.Name = timestamp2 + "." + final
		img.Path = path
		img.Pathname = osspath
		img.Addtime = sword.Timeget()
		img.Type = 1
		id, err := models.Orm().Insert(&img)
		if err != nil {
			beego.Debug("insert media数据失败:", err)
			js["status"] = 0
		} else {
			js["status"] = 1
			js["mid"] = id
			js["src"] = osspath
		}
	}
}

type SwordUppic struct {
	beego.Controller
}

//根据上传的地方进行分类存放图片
func (c *SwordUppic) Post() {
	beego.Debug(111)
	defer c.ServeJSON()
	var js = make(map[string]interface{})
	c.Data["json"] = &js
	f, n, errup := c.GetFile("file")
	if errup != nil {
		beego.Debug("上传文件失败:", errup)
		js["status"] = 0
	} else {
		defer f.Close()
		final := strings.Split(n.Filename, ".")[1]
		timestamp := time.Now().UnixNano()
		timestamp2 := strconv.FormatInt(timestamp, 10)
		timestamp11 := time.Now().Unix()
		tm := time.Unix(timestamp11, 0)
		dirname := tm.Format("2006-01-02")
		var where string
		if c.GetString("where") == "" {
			where = "default"
		} else {
			where = c.GetString("where")
		}
		savepath, _, path, dirpath := Dir(where, dirname, timestamp2, final)
		if errdir := os.MkdirAll(dirpath, 0777); errdir != nil {
			beego.Debug(errdir)
		}
		c.SaveToFile("file", savepath)
		// defer os.Remove(localpath)
		osspath, err := models.OssUpfile(savepath, savepath)
		var img models.Mt_resources
		sess := c.GetSession("home")
		if sess != nil {
			img.Uid, _ = strconv.Atoi(sess.(string))
		} else {
			img.Uid = 0
		}
		img.Ip = strings.Split(c.Ctx.Request.RemoteAddr, ":")[0]
		img.Name = timestamp2 + "." + final
		img.Path = path
		img.Pathname = osspath
		img.Addtime = sword.Timeget()
		img.Type = 0
		id, err := models.Orm().Insert(&img)
		if err != nil {
			beego.Debug("insert media数据失败:", err)
			js["status"] = 0
		} else {
			js["status"] = 1
			js["mid"] = id
			js["pathname"] = osspath
		}
	}
}

type WximgAjax struct {
	beego.Controller
}

func (c *WximgAjax) Post() {
	f, _, errup := c.GetFile("img")
	if errup != nil {
		beego.Debug("上传文件失败:", errup)
	}
	defer f.Close()
	timestamp := time.Now().UnixNano()
	timestamp2 := strconv.FormatInt(timestamp, 10)
	timestamp11 := time.Now().Unix()
	tm := time.Unix(timestamp11, 0)
	dirname := tm.Format("2006-01-02")
	var where string
	if c.GetString("where") == "" {
		where = "default"
	} else {
		where = c.GetString("where")
	}
	savepath, _, path, dirpath := Dir(where, dirname, timestamp2, "png")
	if errdir := os.MkdirAll(dirpath, 0777); errdir != nil {
		beego.Debug(errdir)
	}
	c.SaveToFile("img", savepath)
	// defer os.Remove(localpath)
	osspath, err := models.OssUpfile(savepath, savepath)
	var img models.Mt_resources
	sess := c.GetSession("home")
	if sess != nil {
		img.Uid, _ = strconv.Atoi(sess.(string))
	} else {
		img.Uid = 0
	}
	img.Ip = strings.Split(c.Ctx.Request.RemoteAddr, ":")[0]
	img.Name = timestamp2 + ".png"
	img.Path = path
	img.Pathname = osspath
	img.Addtime = sword.Timeget()
	id, err := models.Orm().Insert(&img)
	if err != nil {
		beego.Debug("insert media数据失败:", err)
		c.Data["json"] = map[string]interface{}{"status": 0}
	} else {
		c.Data["json"] = map[string]interface{}{"status": 1, "mid": id, "src": osspath}
	}
	c.ServeJSON()
}
